﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

/**
  *  注册表相关数据
  *  创建时间：2020/01/01 wj
  *  修改时间：2020/03/01 wj
  */
namespace AnalysisSystemLog.Define.Reg
{
    public class RegData
    {
        public RegData(string url) 
        {
            this.GetRegPathGroup(url);
            bool vaild = true;
            this.GetRegRegistryHeader(UrlGroup[0], ref vaild);
            // 根据查找结果，自动分析定位
            if (!vaild || this.SetRegRegistryKey())
            {
                this.IsRegSuccess = false;
            }
        }      

        // 地址路径查询情况
        public bool isRegSuccess = true;
        public bool IsRegSuccess
        {
            get { return isRegSuccess; }
            set
            {
                isRegSuccess = value;
            }
        }

        // 注册表路径头文件
        private RegistryKey regHeader = Registry.CurrentUser;
        public RegistryKey RegHeader
        {
            get { return regHeader; }
            set
            {
                regHeader = value;
            }
        }

        // 注册表路径
        private string regUrl = String.Empty;
        public string RegUrl 
        {
            get { return regUrl; }
            set
            {
                regUrl = value;
            }
        }

        // 注册表路径地址组
        private static string[] urlGroup = new String[] { };
        public string[] UrlGroup
        {
            get { return urlGroup; }
            set
            {
                urlGroup = value;
            }
        }


        /**
         * \brief 注册表路径转换
         * 
         * \param url 注册表位置路径 
         */
        public void GetRegPathGroup(string url)
        {
            if (url.StartsWith("[")) 
            {
                if (url.IndexOf("]")> 0)
                {
                    url = url.Substring(1, url.IndexOf("]") - 2);
                }
                else
                {
                    url = url.Substring(1, url.Length - 1);
                }
            }
            // 统一路径格式
            url = url.Replace("\\","/");
            urlGroup = url.Split('/');
        }

        /** 
         * \brief 注册表路径转换 
         * 
         * \param name 注册表路径头名称
         * \param ref vaild 注册表路径头寻找结果
         */
        private RegistryKey GetRegRegistryHeader(string name, ref bool vaild)
        {
            RegistryKey mainSource = Registry.CurrentUser ;
            switch (name) {
                case "HKEY_CLASSES_ROOT":
                    mainSource = Registry.ClassesRoot;
                    break;
                case "HKEY_CURRENT_CONFIG":
                    mainSource = Registry.CurrentConfig;
                    break;
                case "HKEY_CURRENT_USER":
                    mainSource =  Registry.CurrentUser;
                    break;
                case "HKEY_DYN_DATA":
                    mainSource = Registry.DynData;
                    break;
                case "HKEY_LOCAL_MACHINE":
                    mainSource = Registry.LocalMachine;
                    break;
                case "HKEY_PERFORMANCE_DATA":
                    mainSource = Registry.PerformanceData;
                    break;
                case "HKEY_USERS":
                    mainSource = Registry.Users;
                    break;
               default:
                    break;
            }
            return mainSource;
        }

        /**
           * \brief 创建各设备类的存储字典
           * 
           * \param[in] string 设备类型，对应Reg中的summary
           */
        public bool SetRegRegistryKey()
        {
                bool vaild = true;
                RegistryKey mainSource = GetRegRegistryHeader(urlGroup[0], ref vaild);
                RegistryKey lastRegKey = mainSource;
                RegistryKey curreentRegKey = mainSource;
                if (vaild) 
                {
                    try
                    {
                        for (int i = 1; i < urlGroup.Length; i++)
                        {
                            curreentRegKey = lastRegKey.OpenSubKey(urlGroup[i], true);
                            lastRegKey = curreentRegKey;
                        }
                        mainSource.SetValue("value", RegUrl);
                    }
                    catch
                    {
                        vaild = false;
                    }
                }
                return vaild;
         }
    }
}
